package com.joysuch.wwyt.risk.service.impl;

import com.joysuch.wwyt.risk.bean.RiskStableEvalutionBean;
import com.joysuch.wwyt.risk.entity.RiskStableEvalution;
import com.joysuch.wwyt.risk.repository.RiskStableEvalutionDao;
import com.joysuch.wwyt.risk.service.RiskStableEvalutionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Optional;

@Slf4j
@Service
@Transactional
public class RiskStableEvalutionServiceimpl implements RiskStableEvalutionService {

    @Autowired
    private RiskStableEvalutionDao dao;

    @Override
    public RiskStableEvalution add(RiskStableEvalution entity) {
        Long id = entity.getId();
        RiskStableEvalutionBean bean = new RiskStableEvalutionBean();
        if (id == null) {
            RiskStableEvalution save = dao.save(entity);
            return save;
        } else {
            return null;
        }

    }

    @Override
    public RiskStableEvalution update(RiskStableEvalution re) {
        Long id = re.getId();
        if (id != null) {
            RiskStableEvalution bean = dao.findById(id).get();
            bean.setId(id);
           /* bean.setRiskEvalutionId(re.getRiskEvalutionId());
            bean.setRiskDistrictId(re.getRiskDistrictId());*/
            bean.setAreaCategory(re.getAreaCategory());
            bean.setChemicalCategory(re.getChemicalCategory());
            bean.setProcessAndChemical(re.getProcessAndChemical());
            bean.setProcessPressure(re.getProcessPressure());
            bean.setProcessTemperature(re.getProcessTemperature());
            bean.setAccident(re.getAccident());
            bean.setPeopleTotalNumber(re.getPeopleTotalNumber());
            bean.setAreaMaxNumber(re.getAreaMaxNumber());
            bean.setEnterpriseMaxNumber(re.getEnterpriseMaxNumber());
            bean.setMaxLevel(re.getMaxLevel());
            bean.setScope(re.getScope());
            bean.setRiskLevel(re.getRiskLevel());
            RiskStableEvalution save = dao.save(bean);
            return save;
        }
        return re;
    }

    @Override
    public RiskStableEvalutionBean findOne(RiskStableEvalution entity) {
        Long id = entity.getId();
        Optional<RiskStableEvalution> byId = dao.findById(id);
        RiskStableEvalutionBean bean;
        RiskStableEvalution re;
        if (byId.isPresent()) {
            bean = new RiskStableEvalutionBean();
            re = byId.get();
            bean.setId(re.getId());
            bean.setAreaCategory(re.getAreaCategory());
            bean.setChemicalCategory(re.getChemicalCategory());
            bean.setProcessAndChemical(re.getProcessAndChemical());
            bean.setProcessPressure(re.getProcessPressure());
            bean.setProcessTemperature(re.getProcessTemperature());
            bean.setAccident(re.getAccident());
            bean.setPeopleTotalNumber(re.getPeopleTotalNumber());
            bean.setAreaMaxNumber(re.getAreaMaxNumber());
            bean.setEnterpriseMaxNumber(re.getEnterpriseMaxNumber());
            bean.setMaxLevel(re.getMaxLevel());
            bean.setScope(re.getScope());
            bean.setRiskLevel(re.getRiskLevel());
            return bean;
        } else {
            //throw new IllegalArgumentException("数据不存在");
            return null;
        }
    }

    @Override
    public void delete(RiskStableEvalution entity) {
        dao.deleteById(entity.getId());
        // dao.delete(entity);
    }
}
